import { verify } from '@/Utils/request';
import { createRouter, createWebHashHistory } from 'vue-router';
import Home from 'views/Home/Home.vue';
import type { AnyObj } from '@/Utils/type';

interface BreadcrumbNameParamsType {
	params?: AnyObj;
}

const router = createRouter({
	history: createWebHashHistory(),
	routes: [
		{
			path: '/',
			component: Home,
			redirect: () => {
				return {
					path: '/paperList',
					name: 'PaperList'
				}
			},
			children: []
		},
		{
			path: '/home',
			name: 'Home',
			component: Home,
			children: [
				{
					path: '/updateTopic/:id',
					name: 'UpdateTopic',
					component: () => import('views/UpdateTopic/UpdateTopic.vue'),
					meta: {
						breadcrumbName: ({ params = {} } : BreadcrumbNameParamsType) => {
							if (Number(params.id) > 0) {
								return '编辑试题';
							}
							return '新增试题';
						},
					}
				},
				{
					path: '/topicList',
					name: 'topicList',
					component: () => import('views/TopicList/TopicList.vue'),
					meta: {
						breadcrumbName: '试题列表',
					}
				},
				{
					path: '/paperList',
					name: 'PaperList',
					component: () => import('views/PaperList/PaperList.vue'),
				},
				{
					path: '/savePaper/:id',
					name: 'SavePaper',
					component: () => import('views/SavePaper/SavePaper.vue'),
				},
				{
					path: '/generatepaper',
					name: 'Generatepaper',
					component: () => import('views/Generatepaper/Generatepaper.vue'),
				},
				{
					path: '/saveUser/:userId',
					name: 'SaveUser',
					component: () => import('views/SaveUser/SaveUser.vue'),
				},
				{
					path: '/userList',
					name: 'UserList',
					component: () => import('views/UserList/UserList.vue'),
				}
			]
		},
		{
			path: '/login',
			name: 'Login',
			component: () => import('views/Login/Login.vue')
		},
		{
			path: '/paperView/:id',
			name: 'PaperView',
			component: () => import('views/PaperView/PaperView.vue'),
		}
	]
})

router.afterEach(async (to, from) => {
	if (to.fullPath != '/') {
		// console.log('to: ', to);
		// console.log('from: ', from)
		// console.log(['/login', '/signUp', '/forgotPassword'].includes(to.path));
		if (['/login'].includes(to.path) === false) {
			verify();
		}
	}
});

export default router;